﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Data;
namespace LotteryAdmin
{
    public partial class BetRecord : UserControl
    {
        LotteryAdminClient srv;
        DateTime startDate
        {
            get { return datePickerStart.SelectedDate.Value.Date; }
            set { datePickerStart.SelectedDate = value; }
        }
        DateTime endDate
        {
            get { return datePickerEnd.SelectedDate.Value.Date.AddDays(1).AddSeconds(-1); }
            set { datePickerEnd.SelectedDate = value; }
        }
        public BetRecord()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(BetRecord_Loaded);
            this.cbxLottery.SelectionChanged += new SelectionChangedEventHandler(cbxLottery_SelectionChanged);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }
        void cbxLottery_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            srv.GetPlayTypeInfoByLotteryIdAsync((this.cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID);
            loadingProgress.IsBusy = true;
            srv.GetPlayTypeInfoByLotteryIdCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                List<PlayTypeInfo> playtype = er.Result.ToList();
                playtype.Insert(0, new PlayTypeInfo()
                {
                    PlayTypeName = "请选择",
                    PlayTypeID = 0
                });
                this.cbxPlayType.ItemsSource = playtype;
                this.cbxPlayType.DisplayMemberPath = "PlayTypeName";
                this.cbxPlayType.SelectedValuePath = "PlayTypeID";
                this.cbxPlayType.SelectedIndex = 0;
            };
        }
        void BetRecord_Loaded(object sender, RoutedEventArgs e)
        {
            this.datePickerStart.DisplayDate = System.DateTime.Now.Date;
            this.datePickerStart.SelectedDate = DateTime.Now;
            this.datePickerEnd.SelectedDate = DateTime.Now;
            this.datePickerEnd.DisplayDate = System.DateTime.Now.Date.AddDays(1).AddSeconds(-1);
            InitComboBox();
            
        }
        void LoadGrid()
        {
            if (!Validate.IsZNumber(txtRecordId.Text.ToString()))
            {
                MessageBox.Show("请输入正确的订单号！");
                return;
            }
            this.UpdateLayout();
            string userid = txtUserId.Text.ToString().Trim();
            Int64 lotteryid = (cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID;
            Int64 recordid = (txtRecordId.Text.ToString()==string.Empty)?0:Int64.Parse(txtRecordId.Text.ToString());
            Int64 playtypeid = (cbxPlayType.SelectedItem as PlayTypeInfo).PlayTypeID;
            string isPj = (cbxIsPj.SelectedItem as ComboBoxItem).Tag.ToString() == "-1" ? "" :
                (cbxIsPj.SelectedItem as ComboBoxItem).Tag.ToString();
            string winorlose = (cbxWin.SelectedItem as ComboBoxItem).Tag.ToString() == "-1" ? "" :
                (cbxWin.SelectedItem as ComboBoxItem).Tag.ToString();
            srv = WcfProxy.GetProxy;
            loadingProgress.IsBusy = true;
            srv.GetBetRecordAsync(playtypeid,recordid,lotteryid, isPj, userid, winorlose, startDate.ToString("yyyy-MM-dd HH:mm:ss"), endDate.ToString("yyyy-MM-dd HH:mm:ss"));
            srv.GetBetRecordCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                if (e.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                PagedCollectionView pageView = new PagedCollectionView(e.Result);
                this.dataPager.Source = pageView;
                this.dgList.ItemsSource = pageView;
            };
        }
        void InitComboBox()
        {
            srv = WcfProxy.GetProxy;
            srv.GetAllLotteryInfoAsync();
            loadingProgress.IsBusy = true;
            srv.GetAllLotteryInfoCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                List<LotteryInfo> lottery = e.Result.ToList();
                lottery.Insert(0, new LotteryInfo()
                {
                     LotteryName="请选择",
                     LotteryTypeID=0
                });
                this.cbxLottery.ItemsSource = lottery;
                this.cbxLottery.DisplayMemberPath = "LotteryName";
                this.cbxLottery.SelectedValuePath = "LotteryTypeID";
                this.cbxLottery.SelectedIndex = 0;
                //srv.GetAllPlayTypeInfoAsync();
                List<PlayTypeInfo> playtype = new List<PlayTypeInfo>();
                playtype.Insert(0, new PlayTypeInfo()
                {
                    PlayTypeName = "请选择",
                    PlayTypeID = 0
                });
                this.cbxPlayType.ItemsSource = playtype;
                this.cbxPlayType.DisplayMemberPath = "PlayTypeName";
                this.cbxPlayType.SelectedValuePath = "PlayTypeID";
                this.cbxPlayType.SelectedIndex = 0;
            };
            //srv.GetAllPlayTypeInfoCompleted += (s, e) =>
            //{

            //};

        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateUserID(this.txtUserId.Text))
            {
                MessageBox.Show("用户名只能输入英文、汉字、数字、下划线");
                return;
            }
            if (!Validate.ValidateNums(this.txtRecordId.Text))
            {
                MessageBox.Show("投注单号只能输入纯数字");
                return;
            }
            LoadGrid();
        }
        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dataPager.PageIndex*dataPager.PageCount+e.Row.GetIndex() + 1).ToString();
            (this.dgList.Columns[17].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            if ((e.Row.DataContext as AdminService.BetRecord).ResultStatus == 1)
            {
                (this.dgList.Columns[13].GetCellContent(e.Row) as TextBlock).Text = "已派奖";
                (this.dgList.Columns[13].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
                (this.dgList.Columns[17].GetCellContent(e.Row) as Button).Visibility = Visibility.Collapsed;
            }
            else
            {
                (this.dgList.Columns[13].GetCellContent(e.Row) as TextBlock).Text = "未派奖";
                (this.dgList.Columns[13].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Black);
                (this.dgList.Columns[17].GetCellContent(e.Row) as Button).Visibility = Visibility.Visible;
            }
            if ((e.Row.DataContext as AdminService.BetRecord).IsWin == 1)
            {
                (this.dgList.Columns[14].GetCellContent(e.Row) as TextBlock).Text = "赢";
                (this.dgList.Columns[14].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Red);
            }
            else
            {
                (this.dgList.Columns[14].GetCellContent(e.Row) as TextBlock).Text = "输";
                (this.dgList.Columns[14].GetCellContent(e.Row) as TextBlock).Foreground = new SolidColorBrush(Colors.Black);
            }
            StackPanel sp = this.dgList.Columns[11].GetCellContent(e.Row) as StackPanel;
            if (sp.Children.Count > 2)
            {
                if ((e.Row.DataContext as AdminService.BetRecord).Direction == 1)
                    (sp.Children[1] as TextBlock).Visibility = Visibility.Collapsed;
                else
                    (sp.Children[1] as TextBlock).Visibility = Visibility.Visible;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ChangeBetRecord cb = new ChangeBetRecord();
            cb.ShowChildWindow((sender as Button).Tag as AdminService.BetRecord);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            LotteryAdminClient _srv = WcfProxy.GetProxy;
            _srv.DeleteBetRecordAsync((sender as Button).Tag as LotteryAdmin.AdminService.BetRecord);
            _srv.DeleteBetRecordCompleted += (sr, er) =>
            {
                if (er.Result)
                {
                    PagedCollectionView pageView = (PagedCollectionView)(dgList.ItemsSource);
                    System.Collections.ObjectModel.ObservableCollection<LotteryAdmin.AdminService.BetRecord> list
                        = (System.Collections.ObjectModel.ObservableCollection<LotteryAdmin.AdminService.BetRecord>)pageView.SourceCollection;
                    list.Remove((sender as Button).Tag as LotteryAdmin.AdminService.BetRecord);
                    MessageBox.Show("撤销投注记录操作成功!");
                }
                else
                    MessageBox.Show("撤销投注记录操作失败!");
            };
        }
    }
}
